#include "linechart.h"
#include "qpainter.h"
#include<QDebug>

LineChart::LineChart(QWidget *parent):QWidget(parent)
{
    // 坐标系的最小值和最大值的初始化（X and Y）
    minValueX =0;
    minValueY =0;
    center_x =0;
    center_y=0;

    textColor= QColor(0,0,0);
    lineColor = QColor(255,0,0);
}

LineChart::~LineChart()
{

}

void LineChart::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setRenderHint(QPainter::SmoothPixmapTransform);

    // 获取wiget的长和宽
    int widget_w = this->width();
    int widget_h = this->height();

    minValueX = minValueY =10;
    // 坐标系的长和宽
    int width = widget_w-20;
    int height = widget_h-20;

    zoomFactorX = width/578.0;
    zoomFactorY = height/549.0;

    center_x = widget_w/2;
    center_y = widget_h/2;

    drawHor(&painter, center_y, width);
    drawVer(&painter, center_x, height);

    drawPoints(&painter);
    qDebug()<<"zoomFactorX: "<<zoomFactorX;
}


void LineChart::drawHor(QPainter* painter, float centerY, float width)
{
    painter->save();
    painter->setPen(textColor);
    painter->drawLine(QPointF(minValueX,centerY),QPointF(width+minValueX,centerY));

    // 水平方向的刻度线需要向上, 因此数值要比centerY要小
    float valueY_top = centerY-2;
    // 间隔
    scaleIntervelX = width/100;
    for(int i=0;i<=100;i++)
    {
        if(i%10==0)
        {
            painter->drawText(scaleIntervelX*i+10, centerY, 20, 10, Qt::AlignLeft, QString::number(i-50));
        }

        painter->drawLine(QPointF(scaleIntervelX*i+10,centerY),QPointF(scaleIntervelX*i+10,valueY_top));
    }

    painter->restore();
}

void LineChart::drawVer(QPainter* painter, float centerX, float height)
{
    painter->save();
    painter->setPen(textColor);
    painter->drawLine(QPointF(centerX, minValueY),QPointF(centerX,height+minValueY));

    // 垂直方向的刻度线需要向右, 因此数值要比centerX要大
    float valueX_right = centerX+2;
    scaleIntervelY = height/100;
    for(int i=0;i<=100;i++)
    {
        if(i%10==0)
        {
            painter->drawText(valueX_right+minValueX, scaleIntervelY*i+5, 20, 10, Qt::AlignLeft, QString::number(50-i));
        }
        painter->drawLine(QPointF(centerX,scaleIntervelY*i+minValueY),QPointF(valueX_right,scaleIntervelY*i+minValueY));
    }
    painter->restore();
}


void LineChart::drawPoints(QPainter *painter)
{
    painter->save();
    painter->setPen(lineColor);

    if(points.size()>1)
    {
        for(int i=1; i<points.size(); ++i)
        {
             qDebug()<< "<points[i-1].X:"<<points[i-1].X<<"zoomFactorX:"<<zoomFactorX<<"ScaleInterverlX:"<<scaleIntervelX;
            painter->drawLine(QPointF(points[i-1].X*scaleIntervelX+center_x,-points[i-1].Y*scaleIntervelY+center_y), QPointF(points[i].X*scaleIntervelX+center_x,-points[i].Y*scaleIntervelY+center_y));
        }

    }
    painter->restore();
}

void LineChart::addPoint(float axisY)
{
    if(points.size()==50)
    {
        points.clear();
        index=0;
    }
    MyPoint myPoint={index,axisY};
    points.push_back(myPoint);
    index= index+1;

    qDebug()<< "axisY :"<<axisY;
    update();
}
